package com.duoduo.springboot.shiro.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.duoduo.springboot.shiro.vo.PermissionVO;
import com.duoduo.springboot.shiro.vo.RoleVO;
import com.duoduo.springboot.shiro.vo.UserVO;

@Component
public class ShiroHelper {
	private static Map<Integer, RoleVO> roleDatas = new HashMap<Integer, RoleVO>(0);
	private static Map<Integer, PermissionVO> permissionDatas = new HashMap<Integer, PermissionVO>(0);
	private static Map<Integer, UserVO> userDatas = new HashMap<Integer, UserVO>(0);
	private static Map<Integer, List<Integer>> userRoleMap = new HashMap<Integer, List<Integer>>(0);
	private static Map<Integer, List<Integer>> rolePermissionMap = new HashMap<Integer, List<Integer>>(0);

	public Map<Integer, RoleVO> listRoles() {
		if (CollectionUtils.isEmpty(roleDatas)) {
			roleDatas.put(1, new RoleVO(1, "admin"));
			roleDatas.put(2, new RoleVO(2, "user"));
		}
		return roleDatas;
	}

	public Map<Integer, PermissionVO> listPermissions() {
		if (CollectionUtils.isEmpty(permissionDatas)) {
			permissionDatas.put(1, new PermissionVO(1, "add"));
			permissionDatas.put(2, new PermissionVO(2, "delete"));
			permissionDatas.put(3, new PermissionVO(3, "update"));
			permissionDatas.put(4, new PermissionVO(4, "query"));

			permissionDatas.put(5, new PermissionVO(5, "user:query"));
			permissionDatas.put(6, new PermissionVO(6, "user:edit"));
		}
		return permissionDatas;
	}

	public Map<Integer, UserVO> listUsers() {
		if (CollectionUtils.isEmpty(userDatas)) {
			userDatas.put(1, new UserVO(1, "admin", "888888"));
			userDatas.put(2, new UserVO(2, "cgs", "888888"));
		}
		return userDatas;
	}

	public Map<Integer, List<Integer>> listUserRole() {
		if (CollectionUtils.isEmpty(userRoleMap)) {
			List<Integer> roles = new ArrayList<Integer>(0);
			roles.add(1);
			userRoleMap.put(1, roles);

			roles = new ArrayList<Integer>(0);
			roles.add(2);
			userRoleMap.put(2, roles);
		}
		return userRoleMap;
	}

	public Map<Integer, List<Integer>> listRolePermission() {
		if (CollectionUtils.isEmpty(rolePermissionMap)) {
			List<Integer> permissions = new ArrayList<Integer>(0);
			permissions.add(1);
			permissions.add(2);
			permissions.add(3);
			permissions.add(4);
			permissions.add(5);
			permissions.add(6);
			rolePermissionMap.put(1, permissions);

			permissions = new ArrayList<Integer>(0);
			permissions.add(5);
			permissions.add(6);
			rolePermissionMap.put(2, permissions);
		}
		return rolePermissionMap;
	}

	public List<RoleVO> listRolesByUserId(int userId) {
		List<RoleVO> result = new ArrayList<RoleVO>(0);
		if (listUserRole().containsKey(userId)) {
			List<Integer> ids = userRoleMap.get(userId);
			for (Integer id : ids) {
				result.add(listRoles().get(id));
			}
		}
		return result;
	}

	public List<PermissionVO> listPermissionsByRoleId(int roleId) {
		List<PermissionVO> result = new ArrayList<PermissionVO>(0);
		if (listRolePermission().containsKey(roleId)) {
			List<Integer> ids = rolePermissionMap.get(roleId);
			for (Integer id : ids) {
				result.add(listPermissions().get(id));
			}
		}
		return result;
	}

	public List<PermissionVO> listPermissionsByUserId(int userId) {
		List<PermissionVO> result = new ArrayList<PermissionVO>(0);
		if (listUserRole().containsKey(userId)) {
			List<Integer> ids = userRoleMap.get(userId);
			for (Integer id : ids) {
				result.addAll(listPermissionsByRoleId(id));
			}
		}
		return result;
	}
}
